package com.example.startapp;

//管理员操作
//查询数据



import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.example.startapp.adapter.PurchaseAdapter;
import com.example.startapp.adapter.SalesAdapter;
import com.example.startapp.entity.Purchase;
import com.example.startapp.entity.User;
import com.example.startapp.adapter.UsersAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class AdminActivity extends AppCompatActivity {
    private Button btnViewSales;
    private Button btnViewPurchases;
    private Button btnViewUsers;
    private ListView listView;
    private User users;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin);

        btnViewSales = findViewById(R.id.btn_view_sales);
        btnViewPurchases = findViewById(R.id.btn_view_purchases);
        btnViewUsers = findViewById(R.id.btn_view_users);
        listView=findViewById(R.id.list_item);

        btnViewSales.setOnClickListener(v -> loadSales());
        btnViewPurchases.setOnClickListener(v -> loadPurchases());
        btnViewUsers.setOnClickListener(v -> loadUsers());
    }

    //加载用户数据
    private void loadUsers() {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()//连接到后端，需打开自己的wifi热点，连接，查看IP地址
                .url("http://192.168.239.136:8080/api/admin/users") // 修正了URL字符串
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                runOnUiThread(() -> Toast.makeText(AdminActivity.this, "请求失败", Toast.LENGTH_SHORT).show());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    // Parse the response and update the UI
                    String usersData = response.body().string();
                    Log.e("response", usersData);
                    // Update RecyclerView or ListView with sales data
                    runOnUiThread(() -> {
                        // 处理显示数据的逻辑
                        displayUsersData(usersData);
                    });
                } else {
                    runOnUiThread(() -> Toast.makeText(AdminActivity.this, "加载用户数据失败: " + response.message(), Toast.LENGTH_SHORT).show());
                }
            }
        });
    }

    private void displayUsersData(String usersData) {
        // 处理和显示用户数据数据的逻辑
        // 更新一个RecyclerView或ListView
        Toast.makeText(AdminActivity.this, "用户数据: " + usersData, Toast.LENGTH_SHORT).show();
        Gson gson = new Gson();
        Type listType = new TypeToken<List<User>>() {}.getType();
        List<User> users = gson.fromJson(usersData, listType);

        //p判断是否有用户数据
        if (users == null || users.isEmpty()) {
            runOnUiThread(() -> Toast.makeText(AdminActivity.this, "没有用户数据", Toast.LENGTH_SHORT).show());
            return;
        }

        // 更新ListView
        UsersAdapter adapter = new UsersAdapter(AdminActivity.this, users);
        listView.setAdapter(adapter);
    }



    //加载购买数据
    private void loadPurchases() {
        // 实现加载购买数据的逻辑
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()//连接到后端，需打开自己的wifi热点，连接，查看IP地址
                    .url("http://192.168.239.136:8888/api/admin/purchases") // 修正了URL字符串
                    .build();

            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    e.printStackTrace();
                    runOnUiThread(() -> Toast.makeText(AdminActivity.this, "请求失败", Toast.LENGTH_SHORT).show());
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (response.isSuccessful()) {
                        // Parse the response and update the UI
                        String purchasesData = response.body().string();
                        Log.e("response", purchasesData);
                        // Update RecyclerView or ListView with sales data
                        runOnUiThread(() -> {
                            // 处理显示数据的逻辑
                            displaypurchasesData(purchasesData);
                        });
                    } else {
                        runOnUiThread(() -> Toast.makeText(AdminActivity.this, "加载用户数据失败: " + response.message(), Toast.LENGTH_SHORT).show());
                    }
                }
            });
        }

        private void displaypurchasesData(String purchasesData) {
            // 处理数据数据的逻辑
            Toast.makeText(AdminActivity.this, "购买数据: " + purchasesData, Toast.LENGTH_SHORT).show();
            Gson gson = new Gson();
            Type listType = new TypeToken<List<Purchase>>() {}.getType();
            List<Purchase> purchases = gson.fromJson(purchasesData, listType);

            //p判断是否有购买数据
            if (purchases == null || purchases.isEmpty()) {
                runOnUiThread(() -> Toast.makeText(AdminActivity.this, "没有购买数据数据", Toast.LENGTH_SHORT).show());
                return;
            }

            // 更新ListView
            PurchaseAdapter adapter = new PurchaseAdapter(AdminActivity.this, purchases);
            listView.setAdapter(adapter);


    }




    private void loadSales() {
        // 实现加载销售数据的逻辑
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder() //连接到后端，需打开自己的wifi热点，连接，查看IP地址
                .url("http://192.168.239.136:8080/api/admin/getAllSales") // 修正了URL字符串
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                runOnUiThread(() -> Toast.makeText(AdminActivity.this, "请求失败", Toast.LENGTH_SHORT).show());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    // Parse the response and update the UI
                    //请求的数据
                    String SalesData = response.body().string();
                    Log.e("response", SalesData);
                    // Update RecyclerView or ListView with sales data
                    runOnUiThread(() -> {
                        // 处理显示数据的逻辑
                        displaySalesData(SalesData);
                    });
                } else {
                    runOnUiThread(() -> Toast.makeText(AdminActivity.this, "加载销售数据失败: " + response.message(), Toast.LENGTH_SHORT).show());
                }
            }
        });
    }

    private void displaySalesData(String SalesData) {
        // 处理数据数据的逻辑
        Toast.makeText(AdminActivity.this, "销售数据: " +SalesData, Toast.LENGTH_SHORT).show();
        Gson gson = new Gson();
        Type listType = new TypeToken<List<Purchase>>() {}.getType();
        List<Purchase> sales = gson.fromJson(SalesData, listType);

        //p判断是否有购买数据
        if (sales == null || sales.isEmpty()) {
            runOnUiThread(() -> Toast.makeText(AdminActivity.this, "没有销售数据", Toast.LENGTH_SHORT).show());
            return;
        }

        // 更新ListView
        SalesAdapter adapter = new SalesAdapter(AdminActivity.this, sales);
        listView.setAdapter(adapter);

    }
}